package com.casa.POI.dianping;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPOutputStream;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Property;
import org.htmlcleaner.TagNode;


public class FetchListPagesExecutor extends CityThreadsExecutor {

	public FetchListPagesExecutor(int cityId) {
		super(cityId);
	}

	@SuppressWarnings("unchecked")
	@Override
	protected List<? extends Runnable> getNextBatchWorks() {
		Session s = SessionManager.getInstance().openSession();
		Criteria c = s.createCriteria(ListUrl.class);
		c.add(Property.forName("fetched").eq(0));
		c.add(Property.forName("cityId").eq(getCityId()));
		c.setMaxResults(ConfigurationManager.getInstance().getBatchSize());
		List<ListUrl> results = c.list();
		s.close();
		List<Workitem> works = new ArrayList<FetchListPagesExecutor.Workitem>();
		for (ListUrl result : results) {
			works.add(new Workitem(result));
		}
		return works;
	}

	class Workitem implements Runnable {

		private ListUrl listUrl;

		public Workitem(ListUrl listUrl) {
			this.listUrl = listUrl;
		}

		@Override
		public void run() {
			ListData listData = null;
			
			try {
				listData = fetchListData();
			} catch (FileNotFoundException e) {
				Console.newLineWriteLine("%s is not available", listUrl.getUrl());
			} catch (Exception e) {
				e.printStackTrace();
				return;
			}
			
			Session s = SessionManager.getInstance().openSession();
			s.beginTransaction();
			listUrl.setFetched(1);
			s.update(listUrl);
			if (listData != null) s.save(listData);
			s.getTransaction().commit();
			s.close();
			
			if (listData != null) {
				printStatus("fetch " + listData.getUrl());
			}
		}

		private ListData fetchListData() throws IOException {
			GZIPOutputStream gzipOs = null;
			TagNode tn = SpiderUtil.cleanUrl(listUrl.getUrl());
			ByteArrayOutputStream bytesOs = new ByteArrayOutputStream();
			gzipOs = new GZIPOutputStream(bytesOs);
			SpiderUtil.getPrettySerializer().writeToStream(tn, gzipOs, "UTF-8");
			ListData sd = new ListData(listUrl.getUrl(), bytesOs.toByteArray(), getCityId(), 0);
			bytesOs.close();
			gzipOs.close();
			return sd;
		}
	}
	
	public static void main(String[] args) {
		new FetchListPagesExecutor(149).execute();
	}

}
